/*******************************************************************************
 * Copyright 2009 DCSpectrometer - http://code.google.com/p/dcspectrometer 
 *  
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *  
 *     http://www.apache.org/licenses/LICENSE-2.0 
 *     
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 *******************************************************************************/
package com.dcspectrometer.kmeansclustering;

import java.awt.Point;
import java.awt.geom.Point2D;
import java.util.ArrayList;

public class Cluster {
  private Point2D.Double _centroid;
  private double _sumOfDistanceSquares;
  private ArrayList<Point> _points;
  private boolean _recalculateSum;

  public Cluster() {
    this._centroid = null;
    _points = new ArrayList<Point>();
    _recalculateSum = true;
  }

  public void setCentroid(Point2D.Double centroid) {
    _centroid = centroid;
    _recalculateSum = true;
  }

  public Point2D.Double getCentroid() {
    return _centroid;
  }

  public void addPoint(Point newPoint) {
    _points.add(newPoint);
    _recalculateSum = true;
  }

  public void removePoint(Point point) {
    _points.remove(point);
    _recalculateSum = true;
  }

  public int getPointCount() {
    return _points.size();
  }

  public Point getPoint(int index) {
    return _points.get(index);
  }

  public void calcClusterEnergy() {
    _sumOfDistanceSquares = 0;
    for (int i = 0; i < _points.size(); i++) {
      _sumOfDistanceSquares += _points.get(i).distance(_centroid);
    }
  }

  public double getSumOfDistanceSquares() {
    if(_recalculateSum) {
      calcClusterEnergy();
    }
    return _sumOfDistanceSquares;
  }

  public ArrayList<Point> getPoints() {
    return _points;
  }
}
